package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.pojo.WmsOrderChannel;
import com.jy.wms.service.WmsOrderChannelService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;
import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsOrderChannelController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Mon Dec 25 10:44:15 CST 2017
 */
@Controller
@RequestMapping("/wmsorderchannel")
@Api(value = "/wmsorderchannel", description = "订单渠道")
public class  WmsOrderChannelController extends BaseController<WmsOrderChannelService,WmsOrderChannel> {
    @PostMapping(value = "/list")
    @ApiOperation(value = "查询所属货主的订单渠道",response = JSONObject.class, notes = "查询所属货主的订单渠道")
    public @ResponseBody JSONArray getWmsOrderChannel(HttpSession session) {
        List<WmsOrderChannel> list=new ArrayList<WmsOrderChannel>();
        try {
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String  sshzid =hz.get(0).getHzid();
            list= baseService.baseSelectPage(sshzid,ckid);

        }catch (Exception e){
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }

//    @PostMapping(value = "delete")
//    @ApiOperation(value = "删除信息",response = JSONObject.class, notes = "删除信息")
//    public @ResponseBody JsonResult deleteWmsOrderChannel(@RequestBody List<String> ids) {
//        Integer flag=baseService.baseDeleteBathPrimaryKeys(ids);
//        return renderSuccess("删除成功");
//    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增订单渠道",response = JSONObject.class, notes = "新增订单渠道")
    public @ResponseBody JSONObject addWmsOrderChannel(@RequestBody WmsOrderChannel entity,HttpSession session) {
        try {
            entity.setId(UUIDUtils.uuid());
            //去重
            Integer count = baseService.countMcAndBh(entity.getMc(), entity.getCode(),entity.getId());
            if (count == 0) {
                // 获取user
                UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
                //获取仓库id
                entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
                //获取货主id
                List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
                entity.setSshzid(hz.get(0).getHzid());
                if ("".equals(entity.getFjd()) || entity.getFjd() == null) {
                    entity.setFjd("0");
                }
                baseService.baseInsert(entity);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "新增订单渠道成功", "", JSONObject.fromObject(entity).toString());
            } else {
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增订单渠道失败", "渠道编码或名称重复，增加失败", "");
            }
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "增加菜单失败", e.getMessage(), "");
        }
    }

    @GetMapping(value = "getWmsOrderChannel/{id}")
    @ApiOperation(value = "查看单个订单渠道信息",response = JSONObject.class, notes = "GET查看单个订单渠道信息")
    public @ResponseBody JSONObject getWmsOrderChannelById(@PathVariable("id") String id) {
        try {
            WmsOrderChannel entity = baseService.baseSelectByPrimaryKey(id);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "查看单个订单渠道信息失成功", "", JSONObject.fromObject(entity).toString());
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "查看单个订单渠道信息失败", e.getMessage(), "");
        }
    }
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑订单渠道",response = JSONObject.class, notes = "编辑订单渠道")
    public @ResponseBody JSONObject editWmsOrderChannel(@RequestBody WmsOrderChannel entity) {
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "编辑订单渠道成功", "", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "编辑订单渠道失败", e.getMessage(), "");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "编辑订单渠道失败", e.getMessage(), "");
        }
    }
}
